#include "Stdafx.h"
#include "IMEBase.h"

std::string CIMEBase::ms_strOutput;

CIMEBase::CIMEBase(void)
{
}

CIMEBase::~CIMEBase(void)
{
}

bool CIMEBase::Init( HWND hWnd )
{
	m_hWnd = hWnd;

	m_uiCodePage = GetACP();

	return true;
}

LRESULT CIMEBase::MsgProc( UINT message, WPARAM wParam, LPARAM lParam )
{
	switch (message)
	{
	case WM_INPUTLANGCHANGE:
		OnInputLangChange(wParam, lParam);
		break;
	case WM_CHAR:
		OnChar(wParam, lParam);
		break;
	case WM_IME_STARTCOMPOSITION:
		OnStartComposition(wParam, lParam);
		break;
	case WM_IME_COMPOSITION:
		OnComposition(wParam, lParam);
		break;
	case WM_IME_ENDCOMPOSITION:
		OnEndComposition(wParam, lParam);
		break;
	case WM_IME_NOTIFY:
		OnNotify(wParam, lParam);
		break;
	default:
		break;
	}

	return DefWindowProc(m_hWnd, message, wParam, lParam);
}

int CIMEBase::ConvertString(const wchar_t* wText, int wLen, char* text, int len)
{
	if(text == 0) 
	{
		return WideCharToMultiByte(m_uiCodePage, 0, wText, wLen, 0, 0, 0, 0);
	}
	else
	{
		int tLen = WideCharToMultiByte(m_uiCodePage, 0, wText, wLen, 0, 0, 0, 0);

		if(tLen > len) 
			return 0;
		else 
			return WideCharToMultiByte(m_uiCodePage, 0, wText, wLen, text, tLen, 0, 0);
	}

	return 0;
}

int CIMEBase::ConvertString(const char* text, int len, wchar_t* wText, int wLen)
{
	int tLen = MultiByteToWideChar(m_uiCodePage, 0, text, len, 0, 0);

	if(tLen <= wLen)
	{
		return MultiByteToWideChar(m_uiCodePage, 0, text, len, wText, tLen);
	}

	return 0;
}

void CIMEBase::OnInputLangChange(WPARAM wParam, LPARAM lParam)
{
	m_uiCodePage = GetACP();
}

void CIMEBase::OnChar( WPARAM wParam, LPARAM lParam )
{

}

void CIMEBase::OnStartComposition( WPARAM wParam, LPARAM lParam )
{

}

void CIMEBase::OnComposition( WPARAM wParam, LPARAM lParam )
{

}

void CIMEBase::OnEndComposition( WPARAM wParam, LPARAM lParam )
{

}

void CIMEBase::OnNotify( WPARAM wParam, LPARAM lParam )
{

}